'use strict'
let articles = require('./models/articles');
var fs = require('fs');
var request = require('request');
var Crawler = require("crawler");
var c = new Crawler({
    maxConnections: 10,
    // This will be called for each crawled page
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;
            var list = $('#content .article table').toArray();
            list.forEach(element => {
                //str = str.match(/aaa(\S*)fff/)[1];
                let Title = $(element).find('.pl2 a  ').text().trim().replace(/[\n\r]/g, '');
                let str1 = ($(element).find(' .pl  ').text().trim().replace(/(^\s*)|(\s*$)/g, '').split(/\s+/)[0]);
                let str2 = ($(element).find(' .pl  ').text().trim().replace(/(^\s*)|(\s*$)/g, '').split(/\s+/)[1]);
                let Author = str1 + str2;
                var images = res.$(element).find('.nbg >img').attr('src')
                let Bookreview = $(element).find(' .quote').text().trim().replace(/[\n\r]/g, '');
                articles.sync().then(() => {articles.create({title: Title,author: Author, Bookreview: Bookreview,})
                });
                setTimeout(function () {
                    request(images).pipe(fs.createWriteStream("E:/nodejs/node-spride/" + Title + '.jpg'))
                }, 4000);
                // console.log(Title.trim());
            });
        }
        done();
    }
});
for (let a = 0; a < 225; a++) {
    var url = 'https://book.douban.com/top250?start=' + a;
    c.queue(url);
}
